package com.zy.kotlinbaselibrary.log

import android.text.TextUtils
import android.util.Log
import com.zy.BaseLibraryConfig


object XLog {

    var LOG = BaseLibraryConfig.LOG
    var TAG_ROOT = BaseLibraryConfig.LOG_TAG

    fun json(json: String) {
        json(Log.DEBUG, null, json)
    }

    fun json(logLevel: Int, tag: String?, json: String?) {
        if (LOG) {

            var formatJson = LogFormat.formatJson(json)

            formatJson?.let {
                formatJson = LogFormat.formatBorder(arrayOf<String>(it))
            }
            XPrinter.println(logLevel, if (TextUtils.isEmpty(tag)) TAG_ROOT else tag, formatJson)
        }
    }

    fun xml(xml: String) {
        xml(Log.DEBUG, null, xml)
    }


    fun xml(logLevel: Int, tag: String?, xml: String) {
        if (LOG) {

            var formatXml = LogFormat.formatXml(xml)
            formatXml?.let {
                formatXml = LogFormat.formatBorder(arrayOf<String>(it))
            }
            XPrinter.println(logLevel, if (TextUtils.isEmpty(tag)) TAG_ROOT else tag, formatXml)
        }
    }

    fun error(throwable: Throwable) {
        error(null, throwable)
    }

    fun error(tag: String?, throwable: Throwable) {
        if (LOG) {
            val formatError = LogFormat.formatBorder(arrayOf(LogFormat.formatThrowable(throwable)))
            XPrinter.println(Log.ERROR, if (TextUtils.isEmpty(tag)) TAG_ROOT else tag, formatError)
        }
    }

    private fun msg(logLevel: Int, tag: String?, format: String?, vararg args: Any) {
        if (LOG) {
            var formatMsg = format
            formatMsg?.let {
                formatMsg = LogFormat.formatBorder(arrayOf(LogFormat.formatArgs(it, *args)))
            }
            XPrinter.println(logLevel, if (TextUtils.isEmpty(tag)) TAG_ROOT else tag, formatMsg)
        }
    }



    fun d(msg: String?, tag: String = TAG_ROOT, vararg args: Any) {
        msg(Log.DEBUG, tag, msg, *args)
    }


    fun e( msg: String?, tag: String = TAG_ROOT, vararg args: Any) {
        msg(Log.ERROR, tag, msg, *args)
    }


    fun i(msg: String?, tag: String  = TAG_ROOT, vararg args: Any) {
        msg(Log.INFO, tag, msg, *args)
    }
}
